/********************************************************************
Crytek Source File.
Copyright (C), Crytek Studios, 2001-2009.
-------------------------------------------------------------------------
File name:   AIAbstractDebugRenderer.cpp
$Id$
Description: Base class for AI debug renderers

-------------------------------------------------------------------------
History:
- 2009-07-02   : Created by Evgeny Adamenkov

*********************************************************************/

#include "StdAfx.h"
#include "AIAbstractDebugRenderer.h"


const char* CAIAbstractDebugRenderer::szNetworkChannelName = "AIDebugDraw";


//====================================================================
// GetDebugDrawZ
//====================================================================
float CAIAbstractDebugRenderer::GetDebugDrawZ(const Vec3& vPoint, bool bUseTerrainOrWater)
{
	const float g_drawOffset = 0.1f;

	if (bUseTerrainOrWater)
	{
		if (g_drawOffset <= 0.0f)
		{
			return -g_drawOffset;
		}
		I3DEngine *pEngine = gEnv->p3DEngine;
		float terrainZ = pEngine->GetTerrainElevation(vPoint.x, vPoint.y);
		float waterZ = pEngine->GetWaterLevel(&vPoint);
		return max(terrainZ, waterZ) + g_drawOffset;
	}
	else
	{
		return vPoint.z + g_drawOffset;
	}
}

void CAIAbstractDebugRenderer::SetAlphaBlended(bool bOn)
{
	SAuxGeomRenderFlags flags = GetFlags();
	flags.SetAlphaBlendMode(bOn ? e_AlphaBlended : e_AlphaNone);
	SetFlags(flags);
}

void CAIAbstractDebugRenderer::SetBackFaceCulling(bool bOn)
{
	SAuxGeomRenderFlags flags = GetFlags();
	flags.SetCullMode(bOn ? e_CullModeBack : e_CullModeNone);
	SetFlags(flags);
}

void CAIAbstractDebugRenderer::SetDepthTest(bool bOn)
{
	SAuxGeomRenderFlags flags = GetFlags();
	flags.SetDepthTestFlag(bOn ? e_DepthTestOn : e_DepthTestOff);
	SetFlags(flags);
}

void CAIAbstractDebugRenderer::SetDepthWrite(bool bOn)
{
	SAuxGeomRenderFlags flags = GetFlags();
	flags.SetDepthWriteFlag(bOn ? e_DepthWriteOn : e_DepthWriteOff);
	SetFlags(flags);
}

void CAIAbstractDebugRenderer::SetDrawInFront(bool bOn)
{
	SAuxGeomRenderFlags flags = GetFlags();
	flags.SetDrawInFrontMode(bOn ? e_DrawInFrontOn : e_DrawInFrontOff);
	SetFlags(flags);
}

uint32 CAIAbstractDebugRenderer::PopState()
{
	assert(!m_stackFlags.empty());
	m_stackFlags.pop();
	assert(!m_stackFlags.empty());
	SetFlags(m_stackFlags.top());
	return m_stackFlags.size();
}

uint32 CAIAbstractDebugRenderer::PushState()
{
	assert(!m_stackFlags.empty());
	m_stackFlags.push(m_stackFlags.top());
	return m_stackFlags.size();
}

// The descendants need not normally override GetFlags()
SAuxGeomRenderFlags CAIAbstractDebugRenderer::GetFlags()
{
	assert(!m_stackFlags.empty());
	return m_stackFlags.top();
}

// The descendants can override SetFlags() but these implementations
// should call CAIAbstractDebugRenderer::SetFlags
void CAIAbstractDebugRenderer::SetFlags(SAuxGeomRenderFlags flags)
{
	assert(!m_stackFlags.empty());
	m_stackFlags.top() = flags;
}
